var output = [],id=0,number=0, answer = [], zong = 0;
var ws, spage;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	console.log(spage);
	showWating();
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=physical&a=problem",
		dataType :'json',
		success :function(data){
//			console.log(JSON.stringify(data));
			$('.zong').text(data.length);
			zong = data.length;
			$.each(data, function(key,vo ){
				plus.nativeUI.closeWaiting();
				output[key] = {'id':vo.id,'tid':vo.tid,'title':vo.title};//将获得的值
			});
//			console.log(JSON.stringify(output));
			setTitle(0);
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	$('#next').on('click',function () {//下一题
		if(!$('input:checked').val()){
			toast('你还未选择');
			return;
		}
		answer[number] = {'id':output[number]['id'],'tid':output[number]['tid'],'val':$('input:checked').val()};
		number++;
		if(number < output.length){ 
			setTitle(number); 
//			console.log(number);
		}
		if(number==1){
			$('#prev').show();
		}else if(number == output.length - 1){
			$('#next').hide();
			$('#submit').show();
		}
		if(answer[number]){
			console.log(answer[number]['val']);
			jQuery('#f'+answer[number]['val']).prop("checked",true);
//			$("input[name=radio1][value='"+ answer[number]['val'] +"']").attr('checked','checked');
		}else{
			$('input:checked').attr('checked',false);
		}
	})
	$('#prev').on('click',function(){//上一题
		number--;
		if(number >= 0){
			setTitle(number);
		}
		if(number == 0){
			$('#prev').hide();
		}else if(number == output.length - 2){
			$('#next').show();
			$('#submit').hide();
		}
		console.log(answer[number]['val']);
		jQuery('#f'+answer[number]['val']).prop("checked",true);
//		$("input[name=radio1][value='"+ answer[number]['val'] +"']").attr('checked','checked');
	});
	var z1 = 0, z2 = 0, z3 = 0, z4 = 0, z5 = 0, z6 = 0, z7 = 0, z8 = 0;//各种体质的分数和
	var e1 = [], e2 = [], e3 = [], e4 = [],e5 = [],e6 = [],e7 = [], e8 = [];
	var physical;//体质各分数的结果
	$('#submit').on('click',function () {/*完成,提交 */
		if(!$('input:checked').val()){
			toast('你还未选择');
			return;
		}
		answer[number] = {'id':output[number]['id'],'tid':output[number]['tid'],'val':$('input:checked').val()};
		console.log(JSON.stringify(answer));
		var str1 = '';
		$.each(answer, function(k,v) {
			switch(v.tid){
				case '67' : z1 += parseInt(v.val);e1.push('1');
				break;
				case '68' : z2 += parseInt(v.val);e2.push('1');
				break;
				case '69' : z3 += parseInt(v.val);e3.push('1');
				break;
				case '70' : z4 += parseInt(v.val);e4.push('1');
				break;
				case '71' : z5 += parseInt(v.val);e5.push('1');
				break;
				case '72' : z6 += parseInt(v.val);e6.push('1');
				break;
				case '73' : z7 += parseInt(v.val);e7.push('1');
				break;
				case '74' : z8 += parseInt(v.val);e8.push('1');
				break;
			}
		});
//		console.log(z1+' '+z2+' '+z3 +' '+z4 +' '+z5+' '+z6+' '+z7+' '+z8);
//		console.log(e1.length+' '+e2.length+' '+e3.length +' '+e4.length+' '+e5.length+' '+e6.length+' '+e7.length+' '+e8.length);
		z1 == 0 ? z1 = 0 :z1 = ((parseInt(z1)-parseInt(e1.length))/(parseInt(e1.length)*4))*100;//转化分数=[（原始分-条目数）/（条目数×4）] ×100
		z2 == 0 ? z2 = 0 :z2 = ((parseInt(z2)-parseInt(e2.length))/(parseInt(e2.length)*4))*100;
		z3 == 0 ? z3 = 0 :z3 = ((parseInt(z3)-parseInt(e3.length))/(parseInt(e3.length)*4))*100;
		z4 == 0 ? z4 = 0 :z4 = ((parseInt(z4)-parseInt(e4.length))/(parseInt(e4.length)*4))*100;
		z5 == 0 ? z5 = 0 :z5 = ((parseInt(z5)-parseInt(e5.length))/(parseInt(e5.length)*4))*100;
		z6 == 0 ? z6 = 0 :z6 = ((parseInt(z6)-parseInt(e6.length))/(parseInt(e6.length)*4))*100;
		z7 == 0 ? z7 = 0 :z7 = ((parseInt(z7)-parseInt(e7.length))/(parseInt(e7.length)*4))*100;
		z8 == 0 ? z8 = 0 :z8 = ((parseInt(z8)-parseInt(e8.length))/(parseInt(e8.length)*4))*100;
		physical = Math.round(z1)+','+Math.round(z2)+','+Math.round(z3)+','+Math.round(z4)+','+Math.round(z5)+','+Math.round(z6)+','+Math.round(z7)+','+Math.round(z8);
		console.log(physical);
		if(spage == 'me-family-add.html' || spage == 'me-family-bianji.html'){
			goNewPage('me-family-inf.html',{physical:physical,spage:spage})
		}else{
			goNewPage('tzjc-home-inf.html',{physical:physical})
		}
		setTimeout(function(){
			plus.webview.currentWebview().close();
		},2000);
	})
//	var aaa = [{"id":"12","tid":"68","val":"2"},{"id":"13","tid":"68","val":"4"},{"id":"14","tid":"68","val":"5"},{"id":"15","tid":"68","val":"3"},{"id":"16","tid":"68","val":"2"},{"id":"1","tid":"69","val":"5"},{"id":"2","tid":"69","val":"3"},{"id":"3","tid":"69","val":"1"},{"id":"4","tid":"69","val":"5"},{"id":"5","tid":"69","val":"4"},{"id":"6","tid":"69","val":"3"},{"id":"7","tid":"69","val":"4"},{"id":"8","tid":"69","val":"3"},{"id":"9","tid":"70","val":"3"},{"id":"10","tid":"70","val":"5"},{"id":"11","tid":"70","val":"4"},{"id":"17","tid":"71","val":"3"},{"id":"18","tid":"71","val":"4"},{"id":"19","tid":"71","val":"5"},{"id":"20","val":"3"}];
//	$.each(aaa, function(k,v) {
//		switch(v.tid){
//			case '67' : z1 += parseInt(v.val);e1.push('1');
//			break;
//			case '68' : z2 += parseInt(v.val);e2.push('1');
//			break;
//			case '69' : z3 += parseInt(v.val);e3.push('1');
//			break;
//			case '70' : z4 += parseInt(v.val);e4.push('1');
//			break;
//			case '71' : z5 += parseInt(v.val);e5.push('1');
//			break;
//			case '72' : z6 += parseInt(v.val);e6.push('1');
//			break;
//			case '73' : z7 += parseInt(v.val);e7.push('1');
//			break;
//			case '74' : z8 += parseInt(v.val);e8.push('1');
//			break;
//		}
//	});
//	console.log(z1+' '+z2+' '+z3 +' '+z4 +' '+z5+' '+z6+' '+z7+' '+z8);
//	console.log(e1.length+' '+e2.length+' '+e3.length +' '+e4.length+' '+e5.length+' '+e6.length+' '+e7.length+' '+e8.length);
//	z1 == 0 ? z1 = 0 :z1 = ((parseInt(z1)-parseInt(e1.length))/(parseInt(e1.length)*4))*100;//转化分数=[（原始分-条目数）/（条目数×4）] ×100
//	z2 == 0 ? z2 = 0 :z2 = ((parseInt(z2)-parseInt(e2.length))/(parseInt(e2.length)*4))*100;
//	z3 == 0 ? z3 = 0 :z3 = ((parseInt(z3)-parseInt(e3.length))/(parseInt(e3.length)*4))*100;
//	z4 == 0 ? z4 = 0 :z4 = ((parseInt(z4)-parseInt(e4.length))/(parseInt(e4.length)*4))*100;
//	z5 == 0 ? z5 = 0 :z5 = ((parseInt(z5)-parseInt(e5.length))/(parseInt(e5.length)*4))*100;
//	z6 == 0 ? z6 = 0 :z6 = ((parseInt(z6)-parseInt(e6.length))/(parseInt(e6.length)*4))*100;
//	z7 == 0 ? z7 = 0 :z7 = ((parseInt(z7)-parseInt(e7.length))/(parseInt(e7.length)*4))*100;
//	z8 == 0 ? z8 = 0 :z8 = ((parseInt(z8)-parseInt(e8.length))/(parseInt(e8.length)*4))*100;
//	physical = Math.round(z1)+','+Math.round(z2)+','+Math.round(z3)+','+Math.round(z4)+','+Math.round(z5)+','+Math.round(z6)+','+Math.round(z7)+','+Math.round(z8);
//	console.log(physical);
//	plus.webview.create('tzjc-home-inf.html','tzjc-home-inf.html',{},{physical:physical}).show('slide-in-right'); 
},false)

function setTitle (num) {
	$('#title').text(output[num]['title']);
//	id = output[num]['id'];console.log(id);
	number = num;
	$('#number').text(number+1);
}